package com.pignest.hotel.ui.room;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Div;
import org.zkoss.zul.Image;
import org.zkoss.zul.Label;
import org.zkoss.zul.Vbox;

import com.pignest.hotel.beans.Booking;
import com.pignest.hotel.beans.Living;
import com.pignest.hotel.beans.Room;
import com.pignest.hotel.definition.OpenRoomType;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Global;
import com.pignest.hotel.util.DateUtil;
import com.pignest.hotel.util.UIUtils;

public class RoomView extends Div implements EventListener {
	private static final long serialVersionUID = 407508742467663745L;
	
	public static final int SHOW_LIVING = 1;
	public static final int SHOW_USE_STATUS = 2;
	public static final int SHOW_LIVE_STATUS = 4;
	public static final int SHOW_REPAIR_STATUS = 8;
	public static final int SHOW_SELF_SERVICE = 16;
	public static final int SHOW_FEATURE = 32;
	public static final int SHOW_BOOKING = 64;
	public static final int SHOW_REMARK = 128;
	public static final int SHOW_ROOM_TYPE = 256;
	
	public static final int SHOW_NONE = 0;
	public static final int SHOW_STATUS = SHOW_USE_STATUS | SHOW_LIVE_STATUS | SHOW_REPAIR_STATUS;
	public static final int SHOW_ALL = Integer.MAX_VALUE;
	
	private RoomHistogram histogram;
	private Room room;
	private int showFlags;
	
	public RoomView() {
		addEventListener(Events.ON_CREATE, this);
	}
	
	private void onCreate() {
		update();
	}
	
	public void updateRoom() {
		this.room = Services.getRoomService().getRoom(room.getId());
		update();
	}
	
	@SuppressWarnings("unchecked")
	public void update() {
		List<Component> children = getChildren();
		Label caption = ((Label)getFirstChild());
		if ((showFlags & SHOW_ROOM_TYPE) != 0) {
			String typeName = room.getType().getName();
			if (typeName.length() > 2)
				typeName = typeName.substring(0, 2);
			caption.setValue(typeName + room.getName());
		} else {
			caption.setValue(room.getName());
		}
//		caption.setStyle("color:#" + room.getType().getColor());
		
		Vbox container = (Vbox)children.get(1);		//body
		List<Image> images = container.getFirstChild().getChildren();
		if ((showFlags & SHOW_USE_STATUS) != 0) {
			String useImage = room.getUseStatusImage();
			if (useImage != null) {
				images.get(0).setSrc(Global.IMAGE_PATH_ROOM_STATUS + useImage);
			}
		} else {
			images.get(0).setStyle("visibility:hidden;");
		}
		if ((showFlags & SHOW_LIVE_STATUS) != 0) {
			String liveImage = room.getLiveStatusImage();
			if (liveImage != null) {
				images.get(1).setSrc(Global.IMAGE_PATH_ROOM_STATUS + liveImage);
			}
		} else {
			images.get(2).setStyle("visibility:hidden;");
		}
		if ((showFlags & SHOW_REPAIR_STATUS) != 0) {
			String repairImage = room.getRepairStatusImage();
			if (repairImage != null) {
				images.get(2).setSrc(Global.IMAGE_PATH_ROOM_STATUS + repairImage);
			}
		} else {
			images.get(1).setStyle("visibility:hidden;");
		}
		if ((showFlags & SHOW_LIVING) != 0) {
			Div c = ((Div)container.getChildren().get(1));
			UIUtils.removeAllChildren(c);
			Living living = null;
			Map<String, Living> map = histogram.getLivingMap();
			if (map != null) {
				living = map.get(room.getId());
			}
			living = map != null ? living : Services.getReceptionService().getLiving(room);
			fillLiving(c, living);
		}
		if ((showFlags & SHOW_BOOKING) != 0) {
			Div c = ((Div)container.getChildren().get(2));
			UIUtils.removeAllChildren(c);
			Booking booking = null;
			Map<String, Booking> map = histogram.getBookingMap();
			if (map != null) {
				booking = map.get(room.getName());
			}
			booking = map != null ? booking : Services.getReceptionService().getBooking(room);
			fillBooking(c, booking);
		}
		if ((showFlags & SHOW_SELF_SERVICE) != 0) {
			if (room.isSelfHelp()) {
				UIUtils.addSClass(this, "selfHelp");
			} else {
				UIUtils.removeSclass(this, "selfHelp");
			}
		}
		if ((showFlags & SHOW_FEATURE) != 0) {
			String color = room.getColor2();
			if (color != null) {
				String colorName = Global.getColorName(color);
				if (colorName != null) {
					Label c = (Label)((Component)container.getChildren().get(3)).getChildren().get(0);
					c.setValue(colorName.substring(0, 1));
					c.setStyle("color:#" + color);
				}
			}
		}
		if ((showFlags & SHOW_REMARK) != 0) {
			if (StringUtils.isNotEmpty(room.getRemark()))
				UIUtils.addSClass((Component)container.getChildren().get(3), "remarked");
			else
				UIUtils.removeSclass((Component)container.getChildren().get(3), "remarked");
		}
	}
	
	public static void fillLiving(Component c, Living living) {
		if (living != null) {
			Label label = new Label(living.getCustormName());
			c.appendChild(label);
			OpenRoomType type = OpenRoomType.valueof(living.getOpenRoomType());
			switch (type) {
			case DAY:
				UIUtils.addSClass(c, "living_day");
				break;
			case HALFDAY:
				UIUtils.addSClass(c, "living_halfday");
				break;
			case HOURS:
				UIUtils.addSClass(c, "living_hour");
				break;
			default:
				break;
			}
			
			int overtime = living.getOvertime();
			switch (overtime) {
			case Living.OVERTIME_YES:
				label.setStyle("color:red;");
				break;
			case Living.OVERTIME_WARM:
				label.setStyle("color:#580000;");
				break;
			case Living.OVERTIME_NOTIFY:
				label.setStyle("color:green;");
				break;
			default:
				break;
			}
		} else {
			UIUtils.removeAllSclass(c);
		}
	}
	
	public static void fillBooking(Component c, Booking booking) {
		if (booking != null) {
			long t = booking.getEstimatedTime().getTime();
			long now = System.currentTimeMillis();
			if (t > now && t < now + DateUtil.TWO_DAY) {
				Label label = new Label("预:" + booking.getName());
				c.appendChild(label);
			}
		}
	}
	
	private void onClick() {
		if (histogram != null) {
			histogram.setSelected(this);
		}
	}
	
	public void setSelectable(boolean selectable) {
		if (selectable)
			addEventListener(Events.ON_CLICK, this);
		else
			removeEventListener(Events.ON_CLICK, this);
	}
	
	public void setSelected(boolean selected) {
		if (selected) {
			UIUtils.addSClass(this, "selected");
		} else {
			UIUtils.removeSclass(this, "selected");
		}
	}

	@Override
	public void onEvent(Event event) throws Exception {
		if (Events.ON_CREATE.equals(event.getName()))
			onCreate();
		else if (Events.ON_CLICK.equals(event.getName()))
			onClick();
	}
	
	public Room getRoom() {
		return room;
	}
	public String getRoomId() {
		return room.getId();
	}
	public void setRoom(Room room, RoomHistogram histogram) {
		this.room = room;
		this.histogram = histogram;
	}
	
	public static RoomView get(Room room, RoomHistogram histogram) {
		RoomView rv = (RoomView) Executions.createComponents("/modules/room/roomView.zul", null, null);
		rv.setRoom(room, histogram);
		rv.showFlags = histogram.getShowFlags();
		return rv;
	}
	
}
